﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameCtrl : Singleton<GameCtrl>
{
    #region UI
    public Canvas curCanvas;
    public StartPanel startPanel;
    public LevelPanel levelPanel;
    public PlayPanel playPanel;
    public LoveLoadingPanel loadingPanel;
    public TipPanel tipPanel;
    public FinalLevelPanel finalLevelPanel;
    #endregion

    #region DataBase
    private int tempLevelId = 1;
    public List<LevelData> levelDataList = new List<LevelData>();
    private void InitLevelDataBase()
    {
        tempLevelId = 1;
        
        
        CreateLevelData(tempLevelId, "Level_WhereIsLove");
        CreateLevelData(tempLevelId, "Level_Light_DianXian");
        CreateLevelData(tempLevelId, "Level_Light_DianXian3");
        CreateLevelData(tempLevelId, "Level_9Heart");
        CreateLevelData(tempLevelId, "Level_Light_DianXian2");
        CreateLevelData(tempLevelId, "Level_Light_Fan");
        CreateLevelData(tempLevelId, "Level_NumberLetter");
        CreateLevelData(tempLevelId, "Level_NumberLetter2");
        CreateLevelData(tempLevelId, "Level_9Memory");
        CreateLevelData(tempLevelId, "Level_LightLLK");
        CreateLevelData(tempLevelId, "Level_LightCZ");


        CreateLevelData(tempLevelId, "Level_26EnglishLetters2");
        CreateLevelData(tempLevelId, "Level_CurTime");
        CreateLevelData(tempLevelId, "Level_Light_Switch");
        CreateLevelData(tempLevelId, "Level_Light_SwitchLock");
        CreateLevelData(tempLevelId, "Level_26EnglishLetters");
        CreateLevelData(tempLevelId, "Level_9Numbers");
        CreateLevelData(tempLevelId, "Level_CountDown");
        CreateLevelData(tempLevelId, "Level_176Letters");
        CreateLevelData(tempLevelId, "Level_CenterButton");


        //CreateLevelData(tempLevelId, "Level_7X9Letters");
        //Level_Balloon



        //CreateLevelData(tempLevelId, "Level_6X6Letters");
        //CreateLevelData(tempLevelId, "Level_ClickRotate");
        //CreateLevelData(tempLevelId, "Level_26EnglishLetters3");
        //CreateLevelData(tempLevelId, "Level_HeartShape");
        //CreateLevelData(tempLevelId, "Level_LetterFragment");
        //CreateLevelData(tempLevelId, "Level_HideBackButton");
        //CreateLevelData(tempLevelId, "Level_HideReplayButton");
        //CreateLevelData(tempLevelId, "Level_HideLevelId");
        //CreateLevelData(tempLevelId, "Level_LoveIsACode");
        //CreateLevelData(tempLevelId, "Level_26EnglishLetters4");
        //CreateLevelData(tempLevelId, "Level_3MoveUpLetters");
        //CreateLevelData(tempLevelId, "Level_TextOnNiceKeys");
        //CreateLevelData(tempLevelId, "Level_TwoButton");
        //CreateLevelData(tempLevelId, "Level_LoveRotate");
        //CreateLevelData(tempLevelId, "Level_26EnglishLettersAdd");
        //CreateLevelData(tempLevelId, "Level_10Letters");
        //CreateLevelData(tempLevelId, "Level_FGRotate");
        //CreateLevelData(tempLevelId, "Level_FGScaleIncrease");
        //CreateLevelData(tempLevelId, "Level_CurTime");
        //CreateLevelData(tempLevelId, "Level_HideLevelId2");
        //CreateLevelData(tempLevelId, "Level_4RotateLetters");
        //CreateLevelData(tempLevelId, "Level_9Memory");

        //CreateLevelData(tempLevelId, "Level_20MoveLetters");
        //CreateLevelData(tempLevelId, "Level_9Heart");
        //CreateLevelData(tempLevelId, "Level_HideReplayRotateHeart");
        //CreateLevelData(tempLevelId, "Level_DragObject");
        //CreateLevelData(tempLevelId, "Level_176Letters");
        //CreateLevelData(tempLevelId, "Level_CircleButton");
        //CreateLevelData(tempLevelId, "Level_PixelLove");
        //CreateLevelData(tempLevelId, "Level_1000kg");
        //CreateLevelData(tempLevelId, "Level_9Numbers");
        //CreateLevelData(tempLevelId, "Level_4x4Love");
        //CreateLevelData(tempLevelId, "Level_2LetterOnSide");
        //CreateLevelData(tempLevelId, "Level_Sum");
        //CreateLevelData(tempLevelId, "Level_1000kg2");
        //CreateLevelData(tempLevelId, "Level_ClickLove");
        //CreateLevelData(tempLevelId, "Level_CountDown");
        //CreateLevelData(tempLevelId, "Level_Big2Small");
        ////CreateLevelData(tempLevelId, "Level_Small2Big");
        //CreateLevelData(tempLevelId, "Level_CubeLove");
        //CreateLevelData(tempLevelId, "Level_2Clock");
        //CreateLevelData(tempLevelId, "Level_Jump");
        //CreateLevelData(tempLevelId, "Level_Tap");
        //CreateLevelData(tempLevelId, "Level_CenterButton");
        //CreateLevelData(tempLevelId, "Level_8Memory");
        //CreateLevelData(tempLevelId, "Level_4X9");
        ////51-55
        //CreateLevelData(tempLevelId, "Level_ShakePhone");
        //CreateLevelData(tempLevelId, "Level_DragLove");
        //CreateLevelData(tempLevelId, "Level_7X9Letters");
        //CreateLevelData(tempLevelId, "Level_1010");
        //CreateLevelData(tempLevelId, "Level_Balloon");
        ////56-60
        //CreateLevelData(tempLevelId, "Level_5ButtonHeart");
        //CreateLevelData(tempLevelId, "Level_3X3HeartLine");
        //CreateLevelData(tempLevelId, "Level_Heart4Part");
        //CreateLevelData(tempLevelId, "Level_ThanksPlayers");
        //CreateLevelData(tempLevelId, "Level_Heart2Button");




        //0106

        if (Application.platform == RuntimePlatform.WindowsEditor)
        {
            string levelIdStr = string.Empty;
            int count = levelDataList.Count;
            for (int i = 0; i < count; i++)
            {
                string levelName = levelDataList[i].levelName.Remove(0, 6);
                levelIdStr += string.Format("{0},{1}\n", levelDataList[i].id, levelName);
            }

            Debug.Log("levelidStr : \n" + levelIdStr);
        }
    }

    private LevelData CreateLevelData(int id, string levelName)
    {
        LevelData ld = new LevelData();
        ld.id = id;
        ld.levelName = levelName;
        levelDataList.Add(ld);
        tempLevelId++;
        return ld;
    }

    public LevelData GetLevelData(int id)
    {

        int count = levelDataList.Count;
        for (int i = 0; i < levelDataList.Count; i++)
        {
            if (levelDataList[i].id == id)
            {
                return levelDataList[i];
            }
        }
        return null;
    }
    #endregion

    #region PlayData
    public int maxLevelId;        // 当前可以玩的最大关卡ID
    public Dictionary<int, int> levelStateDic = new Dictionary<int, int>();     // 关卡状态，通过，跳过，未解锁
    //private Dictionary<int, bool> isGetTipDic = new Dictionary<int, bool>();
    public void InitMaxLevelId()
    {
        maxLevelId = PlayerPrefs.GetInt(UConst.Key_Max_Level, 1);   // 关卡ID 从1开始
    }

    private void InitLevelStateDic()
    {
        levelStateDic.Clear();
        int length = levelDataList.Count;
        for (int i = 0; i < length; i++)
        {
            int levelId = levelDataList[i].id;
            int levelState = PlayerPrefs.GetInt(string.Format(UConst.Key_Is_Level_Pass, levelId), UConst.Level_State_Lock);
            levelStateDic.Add(levelId, levelState);
        }
    }

    public void UpdateLevelState(int levelId, int levelState)
    {
        PlayerPrefs.SetInt(string.Format(UConst.Key_Is_Level_Pass, levelId), levelState);
        if (levelStateDic.ContainsKey(levelId))
            levelStateDic[levelId] = levelState;
        else
        {
            levelStateDic.Add(levelId, levelState);
        }
        if (maxLevelId < levelId + 1)
        {
            maxLevelId = levelId + 1;
            PlayerPrefs.SetInt(UConst.Key_Max_Level, maxLevelId);
        }
    }

    public int GetLevelState(int levelId)
    {
        int levelState = UConst.Level_State_Lock;
        levelStateDic.TryGetValue(levelId, out levelState);
        return levelState;
    }

    //private void InitIsGetTipDic()
    //{
    //    isGetTipDic.Clear();
    //    int length = levelDataList.Count;
    //    for (int i = 0; i < length; i++)
    //    {
    //        int id = levelDataList[i].id;
    //        bool getTip = PlayerPrefs.GetInt(string.Format(UConst.Key_IsGetTip, id), 0) > 0;
    //        isGetTipDic.Add(id, getTip);
    //    }
    //}

    //public void UpdateGetTip(int id, bool getTip)
    //{
    //    if (isGetTipDic.ContainsKey(id))
    //        isGetTipDic[id] = getTip;
    //    if (getTip)
    //        PlayerPrefs.SetInt(string.Format(UConst.Key_IsGetTip, id), 1);
    //}

    //public bool IsGetTip(int id)
    //{
    //    bool isGetTip = false;
    //    isGetTipDic.TryGetValue(id, out isGetTip);
    //    return isGetTip;
    //}
    #endregion

    #region GameLogic
    public GameState curGameState;

    private void Start()
    {
        Init();
    }
    private void Init()
    {
        curGameState = GameState.Menu;
        InitLevelDataBase();
        InitMaxLevelId();
        InitLevelStateDic();
        startPanel.Show();
    }

    public void ChangeGameState(GameState gs)
    {
        curGameState = gs;
    }



    #endregion

    #region Tool
    public string GetNextLetter(string letter)
    {
        string nextLetter = string.Empty;
        switch (letter)
        {
            case "a":
                nextLetter = "b";
                break;
            case "b":
                nextLetter = "c";
                break;
            case "c":
                nextLetter = "d";
                break;
            case "d":
                nextLetter = "e";
                break;
            case "e":
                nextLetter = "f";
                break;
            case "f":
                nextLetter = "g";
                break;
            case "g":
                nextLetter = "h";
                break;
            case "h":
                nextLetter = "i";
                break;
            case "i":
                nextLetter = "j";
                break;
            case "j":
                nextLetter = "k";
                break;
            case "k":
                nextLetter = "l";
                break;
            case "l":
                nextLetter = "m";
                break;
            case "m":
                nextLetter = "n";
                break;
            case "n":
                nextLetter = "o";
                break;
            case "o":
                nextLetter = "p";
                break;
            case "p":
                nextLetter = "q";
                break;
            case "q":
                nextLetter = "r";
                break;
            case "r":
                nextLetter = "s";
                break;
            case "s":
                nextLetter = "t";
                break;
            case "t":
                nextLetter = "u";
                break;
            case "u":
                nextLetter = "v";
                break;
            case "v":
                nextLetter = "w";
                break;
            case "w":
                nextLetter = "x";
                break;
            case "x":
                nextLetter = "y";
                break;
            case "y":
                nextLetter = "z";
                break;
            case "z":
                nextLetter = "a";
                break;
            default:
                break;
        }
        return nextLetter;
    }
    public int Letter2Number(string letter)
    {
        int letterNumber;
        switch (letter)
        {
            case "a":
                letterNumber = 1;
                break;
            case "b":
                letterNumber = 2;
                break;
            case "c":
                letterNumber = 3;
                break;
            case "d":
                letterNumber = 4;
                break;
            case "e":
                letterNumber = 5;
                break;
            case "f":
                letterNumber = 6;
                break;
            case "g":
                letterNumber = 7;
                break;
            case "h":
                letterNumber = 8;
                break;
            case "i":
                letterNumber = 9;
                break;
            case "j":
                letterNumber = 10;
                break;
            case "k":
                letterNumber = 11;
                break;
            case "l":
                letterNumber = 12;
                break;
            case "m":
                letterNumber = 13;
                break;
            case "n":
                letterNumber = 14;
                break;
            case "o":
                letterNumber = 15;
                break;
            case "p":
                letterNumber = 16;
                break;
            case "q":
                letterNumber = 17;
                break;
            case "r":
                letterNumber = 18;
                break;
            case "s":
                letterNumber = 19;
                break;
            case "t":
                letterNumber = 20;
                break;
            case "u":
                letterNumber = 21;
                break;
            case "v":
                letterNumber = 22;
                break;
            case "w":
                letterNumber = 23;
                break;
            case "x":
                letterNumber = 24;
                break;
            case "y":
                letterNumber = 25;
                break;
            case "z":
                letterNumber = 26;
                break;
            default:
                letterNumber = 0;
                break;
        }
        return letterNumber;
    }

    public string Number2Letter(int number)
    {
        string letter = string.Empty;
        switch (number)
        {
            case 1:
                letter = "a";
                break;
            case 2:
                letter = "b";
                break;
            case 3:
                letter = "c";
                break;
            case 4:
                letter = "d";
                break;
            case 5:
                letter = "e";
                break;
            case 6:
                letter = "f";
                break;
            case 7:
                letter = "g";
                break;
            case 8:
                letter = "h";
                break;
            case 9:
                letter = "i";
                break;
            case 10:
                letter = "j";
                break;
            case 11:
                letter = "k";
                break;
            case 12:
                letter = "l";
                break;
            case 13:
                letter = "m";
                break;
            case 14:
                letter = "n";
                break;
            case 15:
                letter = "o";
                break;
            case 16:
                letter = "p";
                break;
            case 17:
                letter = "q";
                break;
            case 18:
                letter = "r";
                break;
            case 19:
                letter = "s";
                break;
            case 20:
                letter = "t";
                break;
            case 21:
                letter = "u";
                break;
            case 22:
                letter = "v";
                break;
            case 23:
                letter = "w";
                break;
            case 24:
                letter = "x";
                break;
            case 25:
                letter = "y";
                break;
            case 26:
                letter = "z";
                break;
            default:
                break;
        }

        return letter;
    }
    #endregion

}

public class LevelData
{
    public string levelName;
    public int id;
}


// a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z
// 1  2  3  4  5  6  7  8  9  10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

//a=1,b=2,c=3,d=4,e=5,f=6,g=7,h=8,i=9,j=10,k=11,l=12,m=13,n=14,o=15......t=20




